package com.vrv.common.utils;

import java.util.Stack;

import com.vrv.common.Constants;

import android.app.Activity;
import android.util.Log;
/**
 * 管理activity
 * @author zxb
 *
 */
public class ScreenManager {
    private static final String TAG = ScreenManager.class.getSimpleName();
    private static final boolean DEBUG = true && Constants.DEBUG;
    
	private static Stack<Activity> activityStack;
	private static ScreenManager instance;

	public static ScreenManager getScreenManager() {
		if (null == instance) {
			instance = new ScreenManager();
		}
		return instance;
	}

	// 退出栈顶Activity
	public void popActivity(Activity activity) {
		if (activity != null) {
	    	if (DEBUG) Log.d(TAG, activity + " is finished, ");
	    	
			activity.finish();
			activityStack.remove(activity);
			activity = null;
		}
	}

	// 获得当前栈顶Activity
	public Activity currentActivity() {
		if (0 == activityStack.size()) {
			return null;
		}
		Activity activity = activityStack.lastElement();
		return activity;
	}

	// 将当前Activity推入栈中
	public void pushActivity(Activity activity) {
		if (null == activityStack) {
			activityStack = new Stack<Activity>();
		}
		activityStack.add(activity);
	}

	// 退出栈中所有Activity
	public void popAllActivityExceptOne(Class cls) {
		while (true) {
			Activity activity = currentActivity();
			if (null == activity) {
				break;
			}
			if (activity.getClass().equals(cls)) {
				break;
			}
			popActivity(activity);
		}
	}
}
